import React, {Context, useEffect, useState,} from 'react'
import {BrowserRouter as Router, Route, Switch, Redirect,} from "react-router-dom"

import Home from "../../container/home/home";
import Video from "../../container/operation/video/video";
import Message from "../../container/operation/message/message";
import VipMessage from "../../container/operation/vip-message/vip-message";
import Point from "../../container/operation/point/point";
import Feedback from "../../container/operation/feedback/feedback";
import Topic from "../../container/operation/topic/topic";
import BasketballLiveList from "../../container/basketball/BasketballLiveList/BasketballLiveList";
import WechatModal from "../../container/mobile/wechat-modal/wechat-modal";
import Download from "../../container/mobile/download/download";
import UserTotal from "../../container/user/user-total/user-total";
import Task from "../../container/operation/task/task";
import UserList from "../../container/user/user-list/user-list";
import PushMatch from "../../container/push/PushMatch/PushMatch";
import PushList from "../../container/push/PushList/PushList";
import Article from "../../container/operation/article/article";
import AIKeyword from "../../container/ai/ai-keyword/ai-keyword";
import AiResult from "../../container/ai/ai-result/ai-result";
import Register from "../../container/auth/register/register";
import Login from "../../container/auth/login/login";
import AuthRoute from "../AuthRoute/AuthRoute";
import AdminContainer from "../../container/admin/admin";
import VisitorList from "../../container/user/visitor-list/visitor-list";
import Taboo from "../../container/operation/taboo/taboo";
import Spyder from "../../container/operation/spyder/spyder";
import PlanApply from "../../container/plan/plan_apply/plan_apply";
import PlanQuiz from "../../container/plan/plan_quiz/plan_quiz";
import VipLoginList from "../../container/user/vip-login-list/vip-login-list";
import {auth, GlobalContext} from "../../context";
import LiveList from "../../container/push/LiveList/LiveList";
import MsgModel from "../../model/msg";
import PlanModel from "../../model/plan";
import FeedbackModel from "../../model/feedback";
import UserPieceRecord from "../../container/together/user-piece-record/user-piece-record";
import Gift from "../../container/finance/gift/gift";
import Money from "../../container/finance/money/money";
import UserHitCount from "../../container/user/user-hit-count/user-hit-count";
import BasketballGameList from "../../container/basketball/basketball-game-list/basketball-game-list";
import Vote from "../../container/operation/vote/vote";
import Mall from "../../container/finance/mall/mall";
import MallDeal from "../../container/finance/mall-deal/mall-deal";
import {notification} from "antd";
import Withdraw from "../../container/finance/withdraw/withdraw";
import TopicComments from "../../container/operation/topic-comments/topic-comments";
import Room from "../../container/operation/room/room";
import Player from "../../container/push/player/player";
import AppActivateList from "../../container/user/app-activate-list/app-activate-list";
import Ad from "../../container/mobile/ad/ad"
import EuroPlan from "../../container/push/EuroPlan/EuroPlan";
import VipRoom from "../../container/operation/vip-room/vip-room";
import Question from "../../container/operation/question/question";
import FakeQuestion from "../../container/operation/fake-question/fake-question";

export const setStorage = params => {

    Object.entries(params).map(entry => {
        let [key, value] = entry
        localStorage.setItem(key, value)
    })

}

const useCount = (key, login) => {
    const msgModel = new MsgModel()
    const [count, setCount,] = useState(0)
    const flags = {
        'feedback': 1,
        'pro': 2,
    }


    useEffect(() => {
        if (login) {
            if (flags[key]) {
                msgModel
                    .getDataTotal({Flag: flags[key]})
                    .then(resp => {
                        const total = resp.data.ok
                        setCount(total)
                    })
            } else if (key === 'deal') {
                msgModel
                    .deal()
                    .then(resp => {
                        const total = resp.data.ok
                        setCount(total)
                        if (total > 0) {
                            notification.open({
                                message: '提现审核',
                                description: `有${total}个待审核`,
                                duration: null,
                            })
                        }
                    })
            }
        }
    }, [login])

    return [count, setCount,]
}



const RouteTable = () => {

    const [login, setLogin,] = useState(auth.login)
    const [token, setToken] = useState(auth.token)
    const [username, setUsername] = useState(auth.username)
    const [perm, setPerm] = useState(auth.perm)
    const [refreshToken, setRefreshToken] = useState(auth.token)

    const [proCount,] = useCount('pro', login)
    const [feedbackCount,] = useCount('feedback', login)
    useCount('deal', login)

    useEffect(() => {
        if (proCount > 0) {
            new PlanModel()
                .allApply({
                    Limit: proCount,
                    Offset: 0,
                    Flag: 0,
                })
                .then(resp => {
                    const list = resp.data.ok.filter(d => d.Flag === 3)
                    if (list.length > 0) {
                        notification.open({
                            message: '专家审核',
                            description: `有${list.length}个待审核`,
                            duration: null,
                        })
                    }
                })
        }
    }, [proCount])

    useEffect(() => {
        if (feedbackCount > 0) {
            new FeedbackModel()
                .all({p: -1})
                .then(resp => resp.data.ok.filter(f => f.Reply === ''))
                .then((result) => {
                    if (result.length > 0) {
                        notification.open({
                            message: '用户反馈',
                            description: `有${result.length}个待审核`,
                            duration: null,
                        })
                    }
                })
        }
    }, [feedbackCount])


    const onLoginSuccess = (data) => {
        setLogin(true)
        const token = data.token1
        const refreshToken = data.token2
        const username = data.username
        const perm = data.perm
        setToken(token)
        setRefreshToken(refreshToken)
        setUsername(username)
        setPerm(perm)
        setStorage({
            login: true,
            token: token,
            username: data.username,
            perm: data.perm,
            refreshToken: refreshToken,
        })
    }

    return <GlobalContext.Provider
        value={{
            auth: {
                login: login,
                token: token,
                refreshToken: refreshToken,
                username: username,
                perm: perm,
                onLoginSuccess: onLoginSuccess,
                // setLogin: setLogin,
            },
        }}
    >

        <Router>

            <Switch>

                <Route path='/login' exact component={Login}/>
                <Route path='/register' exact component={Register}/>

                <AuthRoute path='/' exact component={Home}/>

                <AuthRoute path='/app/modal' exact component={WechatModal}/>
                <AuthRoute path='/app/download' exact component={Download}/>
                <AuthRoute path='/app/ad' exact component={Ad}/>

                <AuthRoute path='/operation/video' exact component={Video}/>
                <AuthRoute path='/operation/message' exact component={Message}/>
                <AuthRoute path='/operation/vip-message' exact component={VipMessage}/>
                <AuthRoute path='/operation/point' exact component={Point}/>
                <AuthRoute path='/operation/feedback' exact component={Feedback}/>
                <AuthRoute path='/operation/topic' exact component={Topic}/>
                <AuthRoute path='/operation/topic-comments' exact component={TopicComments}/>
                <AuthRoute path='/operation/vote' exact component={Vote}/>
                <AuthRoute path='/operation/task' exact component={Task}/>
                <AuthRoute path='/operation/taboo' exact component={Taboo}/>
                <AuthRoute path='/operation/spyder' exact component={Spyder}/>
                <AuthRoute path='/operation/vip-room' exact component={VipRoom}/>
                <AuthRoute path='/operation/question' exact component={Question}/>
                <AuthRoute path='/operation/question1' exact component={FakeQuestion}/>

                <AuthRoute path='/finance/money' exact component={Money}/>
                <AuthRoute path='/finance/gift' exact component={Gift}/>
                <AuthRoute path='/finance/mall' exact component={Mall}/>
                <AuthRoute path='/finance/mall-deal' exact component={MallDeal}/>
                <AuthRoute path='/finance/withdraw' exact component={Withdraw}/>

                <AuthRoute path='/together/piece' exact component={UserPieceRecord}/>

                <AuthRoute path='/operation/article' exact component={Article}/>

                <AuthRoute path='/user/user-total' exact component={UserTotal}/>
                <AuthRoute path='/user/user-list' exact component={UserList}/>
                <AuthRoute path='/user/visitor-list' exact component={VisitorList}/>
                <AuthRoute path='/user/app-activate-list' exact component={AppActivateList}/>
                <AuthRoute path='/user/vip-login-list' exact component={VipLoginList}/>
                <AuthRoute path='/user/hit-count' exact component={UserHitCount}/>

                <AuthRoute path='/ai/ai-keyword' exact component={AIKeyword}/>
                <AuthRoute path='/ai/ai-result' exact component={AiResult}/>

                <AuthRoute path='/plan/plan-apply' exact component={PlanApply}/>
                <AuthRoute path='/plan/plan-quiz' exact component={PlanQuiz}/>

                <AuthRoute path='/push/match' exact component={PushMatch}/>
                <AuthRoute path='/push/list' exact component={PushList}/>
                <AuthRoute path='/push/live' exact component={LiveList}/>
                <AuthRoute path='/push/room' exact component={Room}/>
                <AuthRoute path='/push/player' exact component={Player}/>
                <AuthRoute path='/push/euro' exact component={EuroPlan}/>

                <AuthRoute path='/basketball/live' exact component={BasketballLiveList}/>
                <AuthRoute path='/basketball/list' exact component={BasketballGameList}/>

                <AuthRoute path='/admin' exact component={AdminContainer}/>

            </Switch>

        </Router>
    </GlobalContext.Provider >

}

export default RouteTable
